<?
include "../db_config.php";
include "Database.class.php";

$lat = $HTTP_GET_VARS[lat];
$lng = $HTTP_GET_VARS[lng];
$stationId = $HTTP_GET_VARS[stationId];
if($lat == null)
	$lat = 37.493800613034;
if($lng == null)
	$lng = 127.01422373881;

//chaehyun.kr
$key="954c189d955a835c15a8b516181d53eb";
//ozs.kr
//$key = "16d6f5af2dd150b65b530cb1068213b1";

$db = new Database($host_name, $user_name, $db_password, $db_name);
$db->connect();

if($stationId != null)
{
	$update = array();
	$update['lat'] = $lat;
	$update['lng'] = $lng;
	$date = date("Y-m-d H:i:s", time(NULL));
	$update['lastUpdate'] = $date;
	$db->query_update('stationInfo', $update, " stationId =	'$stationId'");
	$r = $db->query_first("select * from stationInfo where stationId =
	'$stationId'");
	echo $r[lastUpdate];
	$db->close();
	exit(0);
}

?>

<HTML>
<HEAD>
<TITLE> Refine Station Position</TITLE>
<style type="text/css">
body{
	margin:0;
	padding:0;
}
table{
	margin:0;
}
#mapCenter{
	position:absolute;
	left:200;
	top:200;
}
</style>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</HEAD>

<BODY>
<SCRIPT LANGUAGE="JavaScript" src="http://map.naver.com/js/naverMap.naver?key=<?=$key?>"></SCRIPT>
<table>
<tr><td valign=top>
<div id='mapContainer' style='width:400px;height:400px'></div>
<div id=mapCenter>
+
</div>
<div id=pos>

</div>
</td>
<td>


<?
$sql = "select * from stationInfo group by stationName having count(*) > 1 order by stationName";

$r = $db->fetch_all_array($sql);
echo "<table border=1>";
$i = 0;
foreach($r as $d)
{
	$sql = "select * from stationInfo where stationName ='".$d[stationName]."'";
	$st = $db->fetch_all_array($sql);
	foreach($st as $s)
	{
		$link = "<a
		href=\"javascript:move($i)\">map</a>";
		printf("<tr id=tr_$i><td>%d</td>
		<td><div id=station_$i>%s</div></td>
		<td><div id=station_name_$i>%s</div></td>
		<td><div id=line_$i>%s</div></td>
		<td><input value=%.12f size=15 id=lat_$i></td>
		<td><input value=%.12f size=15 id=lng_$i></td><td>%s</td>
		<td><button onclick=\"javascript:saveStationInfo($i)\">저장</button>
		<td><div id=update_$i>%s</div></td></tr>",
		$i++,
		$s[stationId], $s[stationName], $s[line], $s[lat], $s[lng], $link,
		$s[lastUpdate]);
	}
}
echo "</table>";
$db->close();

?>
</td>
</tr>
</table>
<SCRIPT LANGUAGE="JavaScript">
<!--

var currentStName = "";
var currentLine = "";
var currentIndex = 0;

function move(i)
{
	var lat = document.getElementById("lat_"+i).value;
	var lng = document.getElementById("lng_"+i).value;
	
	mapObj.setCenter(new NLatLng(lat, lng));
	var s = document.getElementById("station_name_"+i).innerHTML;
	var line = document.getElementById("line_"+i).innerHTML;
	currentStName = s;
	currentLine = line;
	
	var tr = document.getElementById("tr_"+currentIndex);
	tr.style.backgroundColor='white';
	
	currentIndex = i;
	tr = document.getElementById("tr_"+i);
	tr.style.backgroundColor='gray';
	enddrag();
}
function enddrag()
{
	var pos = mapObj.getCenter();
	var l = mapObj.fromTM128ToLatLng(pos);
	var d = document.getElementById("pos");
	d.innerHTML = currentLine + "호선 " +currentStName + +" " +l.lat()+" " + l.lng();
	if(currentIndex != null && currentIndex != undefined)
	{
		document.getElementById("lat_"+currentIndex).value = l.lat();
		document.getElementById("lng_"+currentIndex).value = l.lng();
		d.innerHTML = d.innerHTML + "<button onclick='javascript:saveStationInfo("+currentIndex+")'>저장</button>";
		var n = currentIndex - 1;
		var k = currentIndex + 1;
		d.innerHTML = d.innerHTML + "<BR><button onclick='javascript:move("+n+")'>이전</button>";
		d.innerHTML = d.innerHTML + "<button onclick='javascript:move("+k+")'>다음</button>";
	}
}
function saveStationInfo(i)
{

	var s = document.getElementById("station_"+i).innerHTML;
	var lat = document.getElementById("lat_"+i).value;
	var lng = document.getElementById("lng_"+i).value;
	//window.console.log(lat, lng, s);

	var http = new NXmlhttp();
	http.setType(0);
	//save behaviour is prohibited
	//http.loadhttp('refineStationInfo.php?stationId='+s+'&lat='+lat+'&lng='+lng,saveReturn);
}
function saveReturn(res)
{
	document.getElementById("update_"+currentIndex).innerHTML = res;
	//window.console.log(res);
}


var mapObj = new NMap(document.getElementById('mapContainer'),400,400);
mapObj.setCenterAndZoom(new NLatLng(<?=$lat?>,<?=$lng?>),0);
NEvent.addListener(mapObj, "mouseup", enddrag);
NEvent.addListener(mapObj, "click", enddrag);

var zoom =new NZoomControl();
zoom.setAlign("left");
zoom.setValign("bottom");
mapObj.addControl(zoom);
//mapObj.addControl(new NIndexMap());

enddrag();



//-->
</SCRIPT>

</body>
</html>
